import React, { Component } from 'react';
import Taro from '@tarojs/taro';
import {WebView, Block} from '@tarojs/components'
import {vcmonitor} from "../../utils/tools";
import {http} from "../../utils/http";
import {UserIdentity} from "../../utils/server.interface";
import {IDENTITY} from "../../constants/api";
import {user} from "../../utils/user.service";

export default class WebViewIndex extends Component {
  state: {
    url: string
  } = {
    url: '',
  }

  async componentDidShow() {
    vcmonitor(this)
    let { notCarryToken = '', url = '' } = Taro.getCurrentInstance().router.params
    if (notCarryToken) { // 不携带token
      this.setState({url})
    } else {
      if (await user.checkLogin()) {
        const res = await http.get<UserIdentity>(IDENTITY, {})
        url += '?token='+ http.getToken() + '&userId=' + res.userId
        this.setState({url})
      } else {
        await user.jumpLogin()
      }
    }
  }

  render() {
    const {url} = this.state;
    return (
      <Block>
        {url && <WebView sandbox='allow-forms allow-modals allow-orientation-lock allow-pointer-lock allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-top-navigation' src={url} />}
      </Block>
    );
  }
}
